<#assign basePath=request.contextPath>
<#assign selp=request.getParameter("selp") />
<#assign selc=request.getParameter("selc") />
<#assign sela=request.getParameter("sela") />
<#assign provinceName=request.getParameter("provinceName") />
<#assign cityName=request.getParameter("cityName") />
<#assign areaName=request.getParameter("areaName") />
<#assign proviceVale=(request.getParameter("proviceVale")==null||request.getParameter("proviceVale")=="") ? sring("add",request.getParameter("proviceVale")) />

<script src="${basePath}/bootstrap/js/jquery.min.js" type="text/javascript"></script>
  	<select class="form-control required"  id="<%=selp%>" name="<%=provinceName%>" onchange="changeProvince(this.value,'<%=selc%>','<%=sela%>')">
      	<option value="">请选择省份</option>
    	</select > 
    	<select class="form-control required" id="<%=selc%>" name="<%=cityName%>"  onchange="changeCity(this.value,'<%=sela%>')">
    	  	<option value="">请选择城市</option>
    	</select >                          
    	<select class="form-control required" id="<%=sela%>" name="<%=areaName%>"  onchange="$('#cityCode').val(this.value)">
    	  	<option value="">请选择区/县</option>
    	</select>
 <script type="text/javascript">
 $(function () {
	 initProvince('<%=proviceVale%>','<%=selp%>','<%=selc%>','<%=sela%>',function(){
		  if('<%=proviceVale%>'!='add'){
			    var liProvince=$("#lipv").val();
				var liCity=$("#licv").val();
				var liArea=$("#liav").val();
				changeProvince(liProvince,"selcity","selarea",function(){
					changeCity(liCity,'selarea'); // 市辖区
				}); 
				
			    var coProvince=$("#copv").val();
				var coCity=$("#cocv").val();
				var coArea=$("#coav").val();
				  if(coProvince){
					changeProvince(coProvince,"selcity1","selarea1",function(){
						changeCity(coCity,'selarea1');// 市辖区
					});
				  }

			    var bankProvince=$("#bankpv").val();
				var bankCity=$("#bankcv").val();
				var bankArea=$("#bankav").val();
				  if(bankProvince){
					changeProvince(bankProvince,"selcity2","selarea2",function(){
					changeCity(bankCity,'selarea2'); // 市辖区
					});
				  }
			 } 
	 });
	});
 
//省
 function initProvince(proval,pro,city,area,callback){
 	$.ajax({
 		type:"get",
 		url:'${basePath}/area/provence',
 		dataType:'json',
 		success:function (data){
 			var prohtml="<option value=''>请选择省份</option>";    
 			for (var i=0;i<data.length;i++) {
 				if (proval==data[i].cityCode) {
 					prohtml=prohtml+("<option selected='selected' value="+data[i].cityCode+">"+data[i].name+"<\/option>");
 				}else{
 					prohtml=prohtml+("<option  value="+data[i].cityCode+">"+data[i].name+"<\/option>");
 				}
 			}
 			$("#"+pro).html(prohtml);
 			$("#"+city).html("<option value=''>请选择城市</option>");
 			$("#"+area).html("<option value=''>请选择区/县</option>"); 
 			callback&&callback();
 		 }
 	});
 }

 //市
 function changeProvince(id,city,area,callback){
     $('#cityCode').val(id);
     if(id==''){
    	$("#"+city).html("<option value=''>请选择城市</option>");
 		$("#"+area).html("<option value=''>请选择区/县</option>");
 		$("#"+area).change();
 	}else{
	 	$.ajax({
	 		type:"get",
	 		url:'${basePath}/area/city/'+id,
	 		dataType:'json',
	 		success:function (data){
	 			var cityVal;
	 			if(city=="selcity"){
	 				 cityVal=$("#licv").val();
	 			}else if(city=="selcity1"){
	 				 cityVal=$("#cocv").val();
	 			}else if(city=="selcity2"){
	 				 cityVal=$("#bankcv").val();
	 			}
	 			var cityhtml="<option value=''>请选择城市</option>";                                                                                                                                                                                                                                                              
	 			for (var i=0;i<data.length;i++) {
	 				if (cityVal==data[i].cityCode) {
	 					cityhtml=cityhtml+("<option selected='selected' value="+data[i].cityCode+">"+data[i].name+"<\/option>");
	 				}else{
	 					cityhtml=cityhtml+("<option  value="+data[i].cityCode+">"+data[i].name+"<\/option>");
	 				}
	 			}
	 			$("#"+city).html(cityhtml);
	 			$("#"+area).html("<option value=''>请选择区/县</option>");
	 			$("#"+area).change();
	 			callback&&callback();


	 		 }
 		});
 	}
 }

//区/县
function changeCity(id,area){
	$('#cityCode').val(id);

	//console.log($("#selarea").val())
	if(id==''){
		$("#"+area).html("<option value=''>请选择区/县</option>");
		$("#"+area).change();
	}else{
		$.ajax({
			type:"get",
			url:'${basePath}/area/county/'+id,
			data:{id:id},
			dataType:'json',
			success:function (data){
				var areaVal;
				if(area=="selarea"){
					 areaVal=$("#liav").val();
	 			} else if(area=="selarea1"){
	 				 areaVal=$("#coav").val();
	 			} else if(area=="selarea2"){
	 				 areaVal=$("#bankav").val();
	 			}
				var areahtml="<option value=''>请选择区/县</option>";
				for (var i=0;i<data.length;i++) {
					if(areaVal==data[i].cityCode){
						areahtml=areahtml+("<option selected='selected' value="+data[i].cityCode+">"+data[i].name+"<\/option>");
					}else{
					    areahtml=areahtml+("<option value="+data[i].cityCode+">"+data[i].name+"<\/option>");
					}
				}
				$("#"+area).html(areahtml);
				$("#"+area).change();
				//港澳台 / 广东省东莞市、中山市
				var liProvince=$("#selprovince").val();
				var liCity=$("#selcity").val();
				if(liProvince == "710000" || liProvince == "810000" || liProvince == "820000" || liCity == "441900" || liCity == "442000"){
					var temp = '<option value="'+$("#selcity").find("option:selected").val()+'" selected>'+$("#selcity").find("option:selected").html()+'</option>'
					$("#selarea").append(temp).change();
				}
			 }
		});
	
	}
}

 </script>